package app.kisslinux.cn.utilsforlin.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.Locale;

/**
 * Created by lsk on 2018-12-19 21:31.
 * <p>
 * 用于拱形板材切割的示意图
 *
 * @author 庄壮壮
 */
public class CuttingPlatesExampleView extends View {

    private static final String TAG = "CuttingPlatesExampleVie";

    private Paint paint;
    private Plates plates;
    private Product product;

    private double scale;

    public CuttingPlatesExampleView(Context context) {
        super(context);
    }

    public CuttingPlatesExampleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        plates = new Plates(90);
        product = new Product(150, 50);
    }

    @Override
    protected void onDraw(Canvas canvas) {


        // 确定一个比较好的 比例，根据产品计算
        int max = getMeasuredHeight() > getMeasuredWidth() ? getMeasuredHeight() : getMeasuredWidth();
        scale = max / product.realR / 3;
//        scale = plates.rectF.bottom / plates.width;

        // 初始化 板材 绘制尺寸
        plates.rectF.left = 0;
        plates.rectF.top = 0;
        plates.rectF.right = getMeasuredHeight();
        plates.rectF.bottom = (float) (plates.getWidth() * scale);

        // 计算 产品 中 圆 的绘制大小
        product.r = (int) (scale * product.realR);

        // 计算 产品 中 圆 的绘制位置
        product.x = (int) (plates.rectF.right / 2);
        product.y = (int) ((scale * product.realLength) + plates.rectF.bottom);

        // 绘制 板材
        plates.draw(canvas, paint);
        // 绘制 产品
        product.draw(canvas, paint);
    }

    public void updatePlates(int size) {
        plates.setWidth(size);
    }

    public void updateProduct(int bottomLength, int height) {
        product.setHeight(height);
        product.setBottomLength(bottomLength);
    }

    class Plates {

        RectF rectF;

        private double width = 0;

        Plates(double width) {
            rectF = new RectF();
            this.setWidth(width);
        }

        double getWidth() {
            return width;
        }

        void setWidth(double width) {
            this.width = width;
        }

        void draw(Canvas c, Paint p) {

            // 绘制 板材
            p.setColor(Color.LTGRAY);
            c.drawRoundRect(this.rectF, 0, 0, p);


            // 绘制 板材规格描述
            p.setColor(Color.BLACK);
            p.setTextSize(48);
            c.drawText(String.format(Locale.CHINA, "%.1fcm", width),
                    this.rectF.right / 40,
                    this.rectF.bottom / 2,
                    p);
        }

    }

    class Product {
        int x;
        int y;
        int r;

        Product(double bottomLength, double height) {
            this.setBottomLength(bottomLength);
            this.setHeight(height);
        }

        // This could be calculate.
        double realR = 0;
        double realLength = 0;

        // 下口长度
        private double bottomLength;

        // 高度
        private double height;

        double getBottomLength() {
            return bottomLength;
        }

        void setBottomLength(double bottomLength) {
            this.bottomLength = bottomLength;
            calculateWhenSizeSet();
        }

        double getHeight() {
            return height;
        }

        void setHeight(double height) {
            this.height = height;
            calculateWhenSizeSet();
        }

        void draw(Canvas canvas, Paint paint) {

            // 绘制 产品说明
            paint.setColor(Color.YELLOW);
            canvas.drawCircle(x, y, r, paint);

            // 绘制 产品规格描述
            paint.setColor(Color.BLACK);

            // 绘制 下口 线 描述
            int bottomLineStartX = x - (int) (getBottomLength() / 2 * scale);
            int bottomLineStartY = y - (int) (realLength * scale);
            int bottomLineEndX = x + (int) (getBottomLength() / 2 * scale);
            int bottomLineEndY = y - (int) (realLength * scale);
            canvas.drawLine(bottomLineStartX, bottomLineStartY, bottomLineEndX, bottomLineEndY, paint);
            // 绘制 下口 文字 描述
            paint.setTextSize(36);
            canvas.drawText("下口长度" + this.getBottomLength() + "cm", bottomLineStartX, bottomLineStartY, paint);

            // 绘制 高度 线 描述
            int heightStartX = x;
            int heightStartY = y - (int) (realLength * scale);
            int heightEndX = x;
            int heightEndY = y - (int) (realR * scale);
            canvas.drawLine(heightStartX, heightStartY, heightEndX, heightEndY, paint);
            // 绘制 高度 文字 描述
            canvas.drawText("高度" + this.getHeight() + "cm", heightEndX, heightEndY, paint);

            // 绘制 绳子 线 描述
            int realRStartX = x;
            int realRStartY = y;
            int realREndX = x + (int) (getBottomLength() / 2 * scale);
            int realREndY = y - (int) (realLength * scale);
            canvas.drawLine(realRStartX, realRStartY, realREndX, realREndY, paint);
            // 绘制 绳子 文字 描述
            canvas.drawText(String.format(Locale.CHINA, "线长%.2fcm", realR),
                    realRStartX,
                    realRStartY + 36,
                    paint);

            // 绘制 距离 线 描述
            int realLengthStartX = x;
            int realLengthStartY = y;
            int realLengthEndX = x;
            int realLengthEndY = y - (int) (realLength * scale);
            canvas.drawLine(realLengthStartX, realLengthStartY, realLengthEndX, realLengthEndY, paint);

            // 绘制 距离 文字 描述
            canvas.drawText(String.format(Locale.CHINA, "距离%.2fcm", realLength),
                    realLengthEndX,
                    realLengthEndY + 40,
                    paint);

        }

        private void calculateWhenSizeSet() {
            double half = this.getBottomLength() / 2;
            double h = this.getHeight();
            this.realLength = (half * half - h * h) / (2 * h);
            this.realR = this.realLength + this.height;
        }

    }
}
